//
//  ECMineMessageViewController.m
//  Edu_iOS2018
//
//  Created by apple on 2018/1/6.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#define PaddingOfHeight 12    //灰色中间边框高度

#import "ECMineMessageViewController.h"
#import "ECSystemNotiViewController.h"
#import "ECSystemBulletinViewController.h"
#import "ECPrivateLetterViewController.h"

@interface ECMineMessageViewController ()<UIScrollViewDelegate>
///头部view
@property (nonatomic, strong) UIView *baseview;

@property (nonatomic, strong) UIScrollView *baseScrollView;
@end

@implementation ECMineMessageViewController

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"消息";
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    // 加载头部视图
    [self addHeadView];
    // 加载滚动视图
    [self.view addSubview:self.baseScrollView];
    // 默认加载第0页
    [self showVc:0];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / kScreenWidth;
    // 添加子控制器view
    [self showVc:index];
    
    // 获取对应btn
    ECUButton *currentBtn = (ECUButton*)[self.baseview.superview viewWithTag:index + 10];
    
    // 获取所有对象
    for (id view in self.baseview.subviews) {
        // 获取所有按钮
        if ([view isKindOfClass:[ECUButton class]]) {
            ECUButton *button = (ECUButton *)view;
            if (button.tag != currentBtn.tag) {
                [button setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:0];
            }else{
                [currentBtn setTitleColor:[UIColor colorWithHexString:@"#FF6701"] forState:0];
            }
        }
    }

    // 获取底部线条
    UILabel *viewDemo = (UILabel*)[self.baseview.superview viewWithTag:99];
    [UIView animateWithDuration:0 animations:^{
        viewDemo.frame = CGRectMake((currentBtn.mj_w-kProportion_Width(72))/2, currentBtn.mj_h-kProportion_Width(9), kProportion_Width(72), kProportion_Width(9));
    } completion:^(BOOL finished) {//跨图层移动
        [viewDemo removeFromSuperview];
        [currentBtn addSubview:viewDemo];
    }];
}

// 头部按钮点击,切换视图
-(void)btn_clcik:(ECUButton *)btn{
    
    // 获取所有对象
    for (id view in btn.superview.subviews) {
        // 获取所有按钮
        if ([view isKindOfClass:[ECUButton class]]) {
            ECUButton *button = (ECUButton *)view;
            if (button.tag != btn.tag) {
                [button setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:0];
            }else{
                [btn setTitleColor:[UIColor colorWithHexString:@"#FF6701"] forState:0];
            }
        }
    }
    
    // 获取底部线条
    UILabel *viewDemo = (UILabel*)[btn.superview.superview viewWithTag:99];
    [UIView animateWithDuration:0 animations:^{
        viewDemo.frame = CGRectMake((btn.mj_w-kProportion_Width(72))/2, btn.mj_h-kProportion_Width(9), kProportion_Width(72), kProportion_Width(9));
    } completion:^(BOOL finished) {//跨图层移动
        [viewDemo removeFromSuperview];
        [btn addSubview:viewDemo];
    }];
    
    //添加相应视图 并滚动到当前页面
    [self scrollCurrentViewWithVc:btn.tag - 10];
    
}

//添加相应视图 并滚动到当前页面
-(void)scrollCurrentViewWithVc:(NSInteger)index{
    //加载相应视图
    [self showVc:index];
    // 相应视图滚动到当前
    [UIView animateWithDuration:0.3 animations:^{
        [self.baseScrollView setContentOffset:CGPointMake((index)*kScreenWidth,0) animated:YES];
    }];
}

// 显示控制器的view
- (void)showVc:(NSInteger)index{
    CGFloat offsetX = index * self.baseScrollView.bounds.size.width;
    UIViewController *vc = self.childViewControllers[index];
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    [self.baseScrollView addSubview:vc.view];
    vc.view.frame = CGRectMake(offsetX, 0, self.baseScrollView.bounds.size.width, self.baseScrollView.bounds.size.height);
}

-(UIScrollView *)baseScrollView{
    if (!_baseScrollView) {
        _baseScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.baseview.frame)+kProportion_Height(PaddingOfHeight), kScreenWidth, kScreenHeight-CGRectGetMaxY(self.baseview.frame)-kProportion_Height(PaddingOfHeight))];
        _baseScrollView.contentSize = CGSizeMake(kScreenWidth*2, kScreenHeight-CGRectGetMaxY(self.baseview.frame)-kProportion_Height(PaddingOfHeight));
        _baseScrollView.pagingEnabled = YES;
        _baseScrollView.bounces = YES;
        _baseScrollView.showsHorizontalScrollIndicator = NO;
        _baseScrollView.delegate = self;
        
        // 添加子控制器
        ECSystemNotiViewController *systemNotiVC = [[ECSystemNotiViewController alloc]init];
        [self addChildViewController:systemNotiVC];
        
//        ECSystemBulletinViewController *systemBulletinVC = [[ECSystemBulletinViewController alloc]init];
//        [self addChildViewController:systemBulletinVC];
        
        ECPrivateLetterViewController *privateLetterVC = [[ECPrivateLetterViewController alloc]init];
        [self addChildViewController:privateLetterVC];
        
    }
    return _baseScrollView;
}

-(void)addHeadView{
    NSArray *titleArray = @[@"系统通知",@"私信"];
    self.baseview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kProportion_Height(138))];
    [self.view addSubview:self.baseview];
    self.baseview.backgroundColor = [UIColor whiteColor];
    for (int i = 0; i < titleArray.count; i++) {
        ECUButton *btn = [ECUButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake((kScreenWidth-(titleArray.count-1))/(titleArray.count) * i + i, 0, (kScreenWidth-(titleArray.count-1))/(titleArray.count), self.baseview.mj_h);
        [btn setTitle:titleArray[i] forState:0];
        btn.tag = i + 10;
        [btn addTarget:self action:@selector(btn_clcik:) forControlEvents:UIControlEventTouchUpInside];
        [self.baseview addSubview:btn];
        if (i == 0) {
            [btn setTitleColor:[UIColor colorWithHexString:@"#FF6701"] forState:0];
        }else{
            [btn setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:0];
        }
    }
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake((kScreenWidth-(titleArray.count -1))/(titleArray.count) + 1, self.baseview.mj_h/4, 1, self.baseview.mj_h/2)];
        lineView.backgroundColor = [UIColor colorWithHexString:@"#DCDCDC"];
        [self.baseview addSubview:lineView];
    
    [self.baseview updateConstraintsIfNeeded];
    
    UIView * scrollLine = [[UIView alloc]init];
    [self.baseview addSubview:scrollLine];
    scrollLine.backgroundColor = [UIColor colorWithHexString:@"ff6600"];
    scrollLine.tag = 99;
    scrollLine.frame = CGRectMake((((kScreenWidth-(titleArray.count-1))/titleArray.count)-kProportion_Width(72))/2, self.baseview.mj_h-kProportion_Width(9), kProportion_Width(72), kProportion_Width(9));
    scrollLine.layer.masksToBounds = YES;
    scrollLine.layer.cornerRadius = 2;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
